﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class UserProfile : IUserProfile
    {
        private UserProfileInfo _userProfileInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        #region Implement interface
        public UserProfileInfo UserProfileInfo
        {
            get
            {
                return _userProfileInfo;
            }
            set
            {
                _userProfileInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in UserProfile object");
        }

        

        public UserProfileInfo GetById(string id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var userProfile = datacontext.TB_UserProfile.Where(u => u.UserId == id).FirstOrDefault();
                if (userProfile != null)
                {
                    return userProfile.ToUserProfileInfo();
                }
                return null;
            }
        }
        #endregion
        #region methods
        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.DeleteObject(datacontext.TB_UserProfile.FirstOrDefault(u => u.UserId == _userProfileInfo.UserId));
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var userProfile = datacontext.TB_UserProfile.Where(u => u.UserId == _userProfileInfo.UserId).FirstOrDefault();
                userProfile.BillToAddress1 = _userProfileInfo.BillToAddress1;
                userProfile.BillToAddress2 = _userProfileInfo.BillToAddress2;
                userProfile.BillToCity = _userProfileInfo.BillToCity;
                userProfile.BillToContact = _userProfileInfo.BillToContact;
                userProfile.BillToCountry = _userProfileInfo.BillToCountry;
                userProfile.BillToZip = _userProfileInfo.BillToZip;
                userProfile.CompanyName = _userProfileInfo.CompanyName;
                userProfile.Description = _userProfileInfo.Description;
                userProfile.Email = _userProfileInfo.Email;
                userProfile.Fax = _userProfileInfo.Fax;
                userProfile.FirstName = _userProfileInfo.FirstName;
                userProfile.LastName = _userProfileInfo.LastName;
                userProfile.Phone = _userProfileInfo.Phone;
                userProfile.Sex = _userProfileInfo.Sex;
                userProfile.TaxId = _userProfileInfo.TaxId;
                userProfile.Title = _userProfileInfo.Title;
                return datacontext.SaveChanges();
            }
        }

        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.AddToTB_UserProfile(this._userProfileInfo.ToTB_UserProfile());
                return datacontext.SaveChanges();
            }
        }
        #endregion
    }
}
